package cn.wenhaha.common.exception;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConfigurationProperties
@PropertySource(value = "classpath:error-code.properties",encoding = "utf-8")
public class ApiMSGException extends RuntimeException {

    private Integer code;
    private String message;

    private  static   Map<Integer, String> error;

    public void setError(Map<Integer, String> error) {
        this.error = error;
    }

    public static ApiMSGException build(Integer code){
        return new ApiMSGException(code);
    }

    public static ApiMSGException build(Integer code,String msg){
        return new ApiMSGException(code,msg);
    }

    public ApiMSGException() { }

    //错误码
    private ApiMSGException(Integer code) {
        this.code=code;
        message = error.getOrDefault(code, "未知错误");
    }

    public ApiMSGException(Integer code, String msg){
        this.code=code;
        message = msg;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}
